#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <pthread.h>

#include "enum.h"
#include "zlog/zlog.h"
#include "bash.h"
#include "deamon-ipc.h"

zlog_category_t *c = NULL;


// 定义一个函数来查找是否有名为 rtty 的进程正在运行
pid_t is_rtty_running(void) {
    FILE *fp;
    char path[BUFSIZ];
    char procname[] = "rtty";
    pid_t pid = -1;

    // 使用 pgrep 命令查找进程 ID
    snprintf(path, sizeof(path), "pgrep %s", procname);
    fp = popen(path, "r");

    if (fp != NULL) {
        if (fscanf(fp, "%d", &pid) > 0) {
            pclose(fp);
            return pid;
        }
        pclose(fp);
    }

    return -1; // 没有找到 rtty 进程
}

void start_rtty_if_not_running(void) {
    pid_t pid = is_rtty_running();

    if (pid == -1) {
        zlog_info(c,"Starting rtty...\n");
        // 使用 system() 函数启动 rtty，这里假设 rtty 是可执行文件并且在 PATH 中
        system(RRTY_EXPORT); 
        // 如果 rtty 需要特定参数，可以在这里添加
        system(RTTY_START_CMD); // 后台启动 rtty
    } else {
        zlog_info(c,"rtty is already running with PID: %d\n", pid);
    }
}

void stop_rtty_if_running(void) {
    pid_t pid = is_rtty_running();

    if (pid != -1) {
        zlog_info(c,"Stopping rtty with PID: %d...\n", pid);
        // 发送 SIGTERM 信号给 rtty 进程以请求其正常终止
        kill(pid, SIGTERM);
        // 可选地等待一段时间让进程有机会终止，或者发送 SIGKILL 强制终止
        // 注意：直接使用 SIGKILL (-9) 可能会阻止进程清理资源
        // kill(pid, SIGKILL);
    } else {
        zlog_info(c,"rtty is not running.\n");
    }
}
#include <sys/ipc.h>
#include <sys/msg.h>

int main(void){
    int rc = 0;
    int ret = 0;
    rc = zlog_init("/emmc/yq-test/cfg/zlog.conf");
    if(rc){
        printf("zlog_init failed,cfg dir ./cfg/zlog.conf\n");
        return ZLOG_ERROR_init;
    }
    printf("zlog_init success\n");

    c = zlog_get_category("app");
    // if(NULL == c){
    if(!c){
        printf("zlog_get_category failed\n");
        return ZLOG_ERROR_get_category;
    }
    printf("zlog_get_category success\n");
    int msgId = msgCreate(QueuePathname,  DefaultProjId);
    zlog_info(c, "deamon handle start");

    struct message msg;
    int work = enumWorkTypeNull;
    while(1){
        // get_status(&status);
        //status.work_state
        memset(&msg, 0, sizeof(struct message));
        // ret = msgRecv(msgId, msg, enumMsgTypeWorkType, false);

        // int msgflg = 0;
        // msgflg |= IPC_NOWAIT;
        // ret = msgrcv(msgId, &msg, sizeof(msg.msgText), enumMsgTypeWorkType, msgflg);

        // ret = msgRecv(msgId, msg, enumMsgTypeWorkType, true);

        ret = msgRecvtest(msgId, &msg, enumMsgTypeWorkType, false);
        if(ret >= 0){
            zlog_info(c, "recv ret %d msg id %d work %d", ret, msgId, msg.msgText[0]);
            zlog_info(c, "work change %d to %d", work, msg.msgText[0]);
            work = msg.msgText[0];
            if(work == enumWorkTypeSleep){
                stop_rtty_if_running();
            }

            if(work == enumWorkTypeWakeup){
                start_rtty_if_not_running();
            }
        }
        
        sleep(1);
    }


    zlog_fini();


    return 0;
}